﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace рисоваклка
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Graphics graphics;    
        Bitmap cleaner;       
        Pen pen;              
        Point point;          
        Bitmap bitmap;        
        bool draw = false;    
       
        private void Form1_Load(object sender, EventArgs e)
        {
            
            bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);  
            cleaner = new Bitmap(pictureBox1.Width, pictureBox1.Height); 
            graphics = Graphics.FromImage(bitmap); 
            pen = new Pen(this.ForeColor);         
            graphics.Clear(Color.White);           
            pictureBox1.Image = bitmap;            
        }
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            point = new Point(e.X, e.Y); 
            draw = true; 
          pictureBox1.DrawToBitmap(cleaner, pictureBox1.DisplayRectangle);
        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            draw = false;
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (draw) 
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0: 
                        graphics.DrawLine(pen, e.Location, point); 
                        point = new Point(e.X, e.Y);               
                        pictureBox1.Invalidate();                  
                        break;
                    case 1:
                        graphics.DrawImage(cleaner, 0, 0); 
                      
                        graphics.DrawRectangle(pen, Math.Min(e.X, point.X), Math.Min(e.Y, point.Y), Math.Abs(e.X - point.X), Math.Abs(e.Y - point.Y));
                        pictureBox1.Invalidate(); 
                        break;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK) 
                pen.Color = colorDialog1.Color;               
        }

    }
}
